#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    painter=new mypainter(this);
    QBrush br(Qt::blue);
    painter->setBrush(br);
    label=new QLabel(this);
    combo=new QComboBox(this);
    layout=new QGridLayout(this);
    label->setText(QString("选择绘制"));
    combo->addItem(QString("Line"),mypainter::Line);//下拉框
    combo->addItem(QString("Rectangle"),mypainter::Rectangle);//combo第二参数是用户数据，当用户选择一个项时，可获得这个数据
     connect(combo,QOverload<int>::of(&QComboBox::activated),this,&Widget::disShapeFunc);//重载信号
     QHBoxLayout*mainlayout=new QHBoxLayout(this);
     layout->addWidget(label,0,0);
     layout->addWidget(combo,0,1);
     mainlayout->addWidget(painter);
     mainlayout->addLayout(layout);
     this->setLayout(mainlayout);
     disShapeFunc(combo->currentIndex());

}

Widget::~Widget()
{
    delete ui;
}

void Widget::disShapeFunc(int index)
{
    mypainter::shape sp=mypainter::shape(combo->itemData(index,Qt::UserRole).toInt());
     //从组合框中获取索引为 index 的项的用户数据 Qt::UserRole 指定了要获取的数据角色，这里表示获取用户自定义数据
    painter->setShape(sp);
}

